package com.cskaoyan.service;

import com.cskaoyan.bean.pojo.Footprint;
import com.cskaoyan.bean.pojo.FootprintExample;
import com.cskaoyan.bean.pojo.Goods;
import com.cskaoyan.bean.pojo.GoodsExample;
import com.cskaoyan.bean.vo.wx.FootprintListVO;
import com.cskaoyan.bean.vo.wx.WxFootprintVO;
import com.cskaoyan.dao.FootprintMapper;
import com.cskaoyan.dao.GoodsMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: hdf
 * @class: FootprintServiceImpl
 */
@Service
public class FootprintServiceImpl implements FootprintService {

    @Autowired
    FootprintMapper footprintMapper;
    @Autowired
    GoodsMapper goodsMapper;

    /**
     * @author hdf
     * @description 获取历史足迹
     * @return
     */
    @Override
    public WxFootprintVO footprintList(Integer page, Integer size) {


        PageHelper.startPage(page,size);

        List<FootprintListVO> footprintListVOS = new ArrayList<>();

        //先查询footprint,再通过goodsId查询其他，封装
        FootprintExample footprintExample = new FootprintExample();
        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);
        for (Footprint footprint : footprints) {
            Integer goodsId = footprint.getGoodsId();
            Goods goods = goodsMapper.selectByPrimaryKey(goodsId);
            Date date = new Date();
            FootprintListVO footprintListVO = new FootprintListVO(goods.getBrief(),
                    goods.getPicUrl(),date,goodsId,goods.getName(),footprint.getId(),goods.getRetailPrice());
            footprintListVOS.add(footprintListVO);
        }

        PageInfo<Footprint> footprintPageInfo = new PageInfo<>(footprints);
        return new WxFootprintVO(footprintPageInfo.getPages(),footprintListVOS);
    }

    /**
     * @author hdf
     * @description 长按删除足迹,因为前端逻辑有问题（没有通过deleted数据来显示页面），所以不能逻辑删除。
     * @return
     */
    @Override
    public void footprintDelete(Integer id) {
        // Footprint footprint = new Footprint();
        // footprint.setDeleted(true);
        // footprint.setId(id);
        // footprintMapper.updateByPrimaryKeySelective(footprint);
        footprintMapper.deleteByPrimaryKey(id);
    }
}
